##Function to Recode 0 to 1
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

 extraversion <- mapply(as.numeric,with(wvs_sub,data.frame(
 ext1,
 ext2,
 ext3, 
 ext4, 
 ext5, 
 ext6, 
 ext7, 
 ext8, 
 ext9,
 ext10)))

 extraversionlist <- list()
 a <- 0
 for(i in 1:9){
   a <- a+(ncol(combn(1:10, i)))
 }
 
 b <- 0
 items <- list()
 for(j in 1:9){
   b=b+1 
   combos <- combn(1:10, j)
   for(i in 1:ncol(combos)){
     items[[b]] <- combos
   }}
 
 itemrow <- lapply(items,nrow)
 itemcol <- lapply(items,ncol)
 
 collapsepastecolumns <- function(x)apply(x,2,paste,collapse=",")
 rr <- lapply(collapsepastecolumns,X = items)
 
 numbero <-   unlist(itemcol)
 emptylist <- list()  
 for(i in 2:9){
   emptylist[[i-1]] <- rep(i,numbero[[i-1]])
 }
 
 h=1
 j=2
 i=1
 alphacoefs <- data.frame(matrix(nrow=1012,ncol=4))
 
 for(j in 2:9){
   combos <- combn(1:10, j)
   two <- matrix(nrow=nrow(wvs_sub),ncol=ncol(combos))
   for(i in 1:ncol(combos)){
     two[,i] <-   rowMeans(extraversion[,combos[,i]])
     alphacoefs[h,] <- psych::alpha(extraversion[,combos[,i]],check.keys = TRUE)$total[1:4]
     h=  h+1
   }
   lmcoefs <- matrix(nrow=ncol(combos),ncol=3)
   for(i in 1:ncol(two)){
     lmcoefs[i,1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
     lmcoefs[i,2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
     lmcoefs[i,3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1(two[,i])+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
   }
   extraversionlist[[j]] <- lmcoefs
    }
 lmcoefs <- matrix(ncol=3)
 
 ## 10 item 
 lmcoefs[1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1(rowMeans(extraversion))+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
 lmcoefs[2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1(rowMeans(extraversion))+  wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
 lmcoefs[3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1(rowMeans(extraversion))+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
 
 extraversionlist[[10]] <- lmcoefs
 alphacoefs[1013,] <- psych::alpha(extraversion,check.keys = TRUE)$total[1:4]
 
 
 lmcoefs <- matrix(nrow=10,ncol=3)
 
 for(i in 1:ncol(extraversion)){
   lmcoefs[i,1]   <-   lm(zero1(wvs_sub$wvs_lrplacement)~zero1((extraversion)[,i])+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
   lmcoefs[i,2]   <-   lm(zero1(wvs_sub$wvs_moral)~zero1((extraversion)[,i])+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub $age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
   lmcoefs[i,3]   <-   lm(zero1(wvs_sub$wvs_economicattitudes)~zero1((extraversion)[,i])+  wvs_sub$IPIP_open+wvs_sub$IPIP_con+wvs_sub$IPIP_open+wvs_sub$IPIP_agre+wvs_sub$IPIP_neu+wvs_sub$gender+ wvs_sub$age+ wvs_sub$age2+ wvs_sub$edu2+ wvs_sub$edu3+ wvs_sub$edu4+ wvs_sub$edu5+ wvs_sub$edu6+ wvs_sub$income2+ wvs_sub$income3+ wvs_sub$income4+ wvs_sub$income5+ wvs_sub$income6+ wvs_sub$income7)$coefficients[2]
 }
 extraversionlist[[1]] <- lmcoefs
 
 l <- Map(cbind, extraversionlist, indx = seq_len(length(extraversionlist))) 
 h <- data.frame(do.call(rbind, l))
 head(h )
 names(h) <- c("Left-Right","Moral","Economic Ideology","Index")
 empties <- data.frame(matrix(NA,nrow = 10,ncol = 4))
 colnames(empties) <- colnames(alphacoefs)
 alphacoefs <- rbind(empties,alphacoefs)
 itemstogether <- unlist(rr)
 itemstogether[1023] <- "all"
 numall <- unlist(emptylist)
 outstanding <- data.frame(h,itemstogether,alphacoefs)
 library(reshape2)
 forfig <- melt(outstanding,id.vars=c("Index","itemstogether",names(empties)))
 forfig$trait <- "Extraversion"
